package com.y.architecture.base_refresh_load;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.y.architecture.base_refresh.BaseRefreshActivity;
import com.y.architecture.recycle_util.BaseQuickAdapter;
import com.y.architecture.recycle_util.BaseViewHolder;
import com.y.architecture.util.AFConstant;
import com.y.architecture.util.AFLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by youjingjing on 2017/11/28.
 */

public abstract class BaseRefreshLoadActivity<T> extends BaseRefreshActivity implements IBaseRefreshLoadContact.IBaseRefreshLoadView<T>{
    protected int mCurrPage = AFConstant.REFRESH_FIRST_PAGE;

    protected RecyclerView mRecyclerView;
    protected RecyclerView.LayoutManager mLayoutManager;
    protected BaseQuickAdapter<T, BaseViewHolder> mAdapter;

    protected List<T> mItems;

    private BaseRefreshLoadHelper<T> mRefreshLoadHelper;

    @Override
    public void initRefresh(Bundle saveInstanceStatus) {
        super.initRefresh(saveInstanceStatus);
        initRefreshLoad(saveInstanceStatus);
    }

    @Override
    public void initRefreshLoad(Bundle saveInstanceStatus) {
        mItems = new ArrayList<>();

        mRefreshLoadHelper = new BaseRefreshLoadHelper<>(this);
        mRecyclerView = mRefreshLoadHelper.initRecycleView();

        setLayoutManager();
        setAdapter();
        addDivider();
    }


    @Override
    public void setLayoutManager() {
        mLayoutManager = mRefreshLoadHelper.setDefaultLayoutManager();
        mRecyclerView.setLayoutManager(mLayoutManager);
    }

    @Override
    public void setAdapter() {
        mAdapter = mRefreshLoadHelper.setAdapter();
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public BaseQuickAdapter<T,BaseViewHolder> getGenerateAdapter() {
        return mAdapter;
    }

    @Override
    public boolean isShowDivider() {
        return true;
    }

    @Override
    public void addDivider() {
        mRefreshLoadHelper.addDivider();
    }

    @Override
    public void onEmptyViewClicked(View view) {
        onRefreshBegin(mSwipeRefreshLayout);
    }

    @Override
    public void onRefreshBegin(SwipeRefreshLayout frame) {
        mCurrPage = AFConstant.REFRESH_FIRST_PAGE;

        loadData(mCurrPage);

        //设置下拉监听
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData(mCurrPage);
            }
        });
    }
//    @Override
//    public void onLoadBegin(SpinKitView frame) {
//        mRefreshLoadHelper.onLoadBegin(frame);
//    }

    @Override
    public List<T> getItems() {
        return mItems;
    }

    @Override
    public void refreshLoadComplete(boolean success) {
        if (getAFContext() == null) {
            return;
        }
        if (mCurrPage > AFConstant.REFRESH_FIRST_PAGE) {
            if (!success) {
                mCurrPage--;
            }
             getGenerateAdapter().loadMoreComplete();
        } else {
             getGenerateAdapter().notifyDataSetChanged();
             refreshComplete();
        }
        boolean enableLoadMore = getItems().size() % AFConstant.REFRESH_DEFAULT_SIZE == 0;
        getGenerateAdapter().setEnableLoadMore(enableLoadMore && success);
    }

    @Override
    public boolean isSuccess(List<T> backData) {
        return backData != null && backData.size() > 0;
    }

    @Override
    public void onLoadMoreRequested() {
        loadData(++mCurrPage);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        AFLog.d("item->" + position + " clicked");
    }


}
